<template>
  <div>
    <el-dialog
      v-model="props.visible"
      title="生产单列表"
      width="100%"
      @close="handleCancel"
    >
      <div v-if="!statusFlag" style="padding-bottom: 40px">
        <el-form
          :model="queryParams"
          class="flex-box"
          ref="queryRef"
          :inline="true"
        >
          <div>
            <el-form-item label="生产单编码" prop="produceNo">
              <el-input
                v-model.trim="queryParams.produceNo"
                placeholder="请输入生产单编码"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">
                搜索
              </el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </div>
        </el-form>

        <div v-if="orderList.length > 0">
          <div
            class="card"
            v-for="item in orderList"
            :key="item.produceNo"
            @click="handleCellClick(item)"
          >
            <h3 class="card__title">生产单编码: {{ item.produceNo }}</h3>
            <div class="card__content">出版编码: {{ item.pubNo }}</div>
            <div class="card__date">交货日期: {{ item.deliverDate }}</div>
            <div class="card__arrow">
              {{ item.type == 1 ? "封面" : "内文" }}
            </div>
          </div>
        </div>
        <div v-else style="text-align: center">暂无数据</div>
        <div>
          <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </div>
      <div v-else>
        <el-page-header @back="goBack">
          <template #content>
            <el-checkbox
              v-model="allChecked"
              @change="changeAll"
              class="text-large font-600 mr-3"
            >
              全选
            </el-checkbox>
          </template>
          <template #extra>
            <el-button
              type="primary"
              plain
              @click="handleSubmit"
              :disabled="multiple"
            >
              确认
            </el-button>
          </template>
        </el-page-header>
        <el-card
          shadow="hover"
          style="margin-top: 10px"
          v-for="(item, index) in paperList"
          :key="item.pieceId"
        >
          <!-- 卡片的头部位 -->
          <template #header>
            <div class="card-header">
              <el-checkbox
                v-model="item.checked"
                :label="item.pieceId"
                @change="changeIds(item)"
              >
                {{ item.producePieceNo }}
              </el-checkbox>
            </div>
          </template>
          <!-- 卡片显示的内容 -->
          <div>
            <div class="card-content">
              <div class="header">
                <div>
                  <a class="title" href="#">
                    {{ item.breedType }}
                  </a>
                </div>
                <span class="image">{{ index + 1 }}</span>
              </div>
              <dl class="post-info">
                <dt class="dt">组成：</dt>
                <dt class="dt">{{ item.composition }}</dt>
              </dl>
              <dl class="post-info">
                <dt class="dt">规格型号：</dt>
                <dt class="dt">
                  <dict-tag
                    :options="mes_paper_specs"
                    :value="item.specification"
                  />
                </dt>
              </dl>
              <dl class="post-info">
                <dt class="dt">令数：</dt>
                <dt class="dt">{{ item.ream }}</dt>
              </dl>
            </div>
          </div>
        </el-card>
        <!-- <el-row :gutter="20" style="overflow: auto">
                  <el-col :span="16">
                      <el-table
                          ref="orderRef"
                          v-loading="loadingInfo"
                          :data="paperList"
                          row-key="pieceId"
                          stripe
                          border
                          style="cursor: pointer"
                          height="420px"
                          @selection-change="handleSelectionChange"
                      >
                          <el-table-column
                              type="selection"
                              :reserve-selection="true"
                              width="55"
                          />
                          <el-table-column label="序号" width="55" type="index" align="center" />
                          <el-table-column
                              label="书刊名称"
                              align="center"
                              
                              prop="bookName"
                          ></el-table-column>
                          <el-table-column
                              label="书刊编码"
                              align="center"
                              
                              prop="bookNo"
                          ></el-table-column>
                          <el-table-column
                              label="印刷方式"
                              align="center"
                              
                              prop="printType"
                          >
                              <template #default="scope">
                                  <dict-tag
                                      :options="mes_paper_package"
                                      :value="scope.row.printType"
                                  />
                              </template>
                          </el-table-column>
                          <el-table-column
                              label="印色"
                              align="center"
                              
                              prop="printColor"
                          >
                              <template #default="scope">
                                  <dict-tag
                                      :options="ems_order_print_color"
                                      :value="scope.row.printColor"
                                  />
                              </template>
                          </el-table-column>
                          <el-table-column
                              label="印张"
                              align="center"
                              
                              prop="sheet"
                          ></el-table-column>
                          <el-table-column
                              label="贴标"
                              align="center"
                              
                              prop="pieceView"
                          ></el-table-column>
                          <el-table-column
                              label="印数"
                              align="center"
                              
                              prop="printAmount"
                          ></el-table-column>
                      </el-table>
                  </el-col>
              </el-row> -->
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { toNumber } from "@/utils/ruoyi";
import {
  getProduceOrderList,
  getProduceById,
  GetProducelistByMachineId,
  getByIdFilter
} from "@/api/production/cover";
const emit = defineEmits(["changeUpdate", "cancel"]);
import useUserStore from "@/store/modules/user";
const userStore = useUserStore();
const { proxy } = getCurrentInstance();
const { mes_paper_specs , mes_paper_package, ems_order_print_color } = proxy.useDict(
  "mes_paper_specs",
  "mes_paper_package",
  "ems_order_print_color"

);

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  orderSelectList: {
    type: Array,
    default: () => {
      [];
    },
  },
  params: {
    type: Object,
    default: () => {
      {
      }
    },
  },
});

const allChecked = ref(false);
const orderList = ref([]);
const paperList = ref([]);
const multiple = ref(true);
const selectRow = ref([]);
const loading = ref(false);
const loadingInfo = ref(false);
const orderListTable = ref(null);
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  type: 1,
  status: "5,6,7,8,9,10,11,12,13,14,15,16,17,18",
});
watch(
  () => props.visible,
  (val) => {
    if (val) {
      handleQuery();
      nextTick(() => {
        // orderRef.value.clearSelection()
        // if (props.orderSelectList.length > 0) {
        //     props.orderSelectList.forEach(item => {
        //         // orderRef.value.toggleRowSelection(item, true)
        //     })
        // }
      });
    }
  }
);

const total = ref(0);
const statusFlag = ref(false);
const goBack = () => {
  statusFlag.value = false;
  selectRow.value = [];
  allChecked.value = false;
};
// 选中当前生产单
const handleCellClick = (row) => {
  statusFlag.value = true;
  handleClick(row);
};
// 修改选中的值
const changeIds = (value) => {
  multiple.value = !paperList.value.filter((item) => item.checked).length;
};
const changeAll = (val) => {
  paperList.value.forEach((item) => {
    item.checked = val;
  });
  multiple.value = !paperList.value.filter((item) => item.checked).length;
};
// 选中订单
const handleClick = (row) => {
  loadingInfo.value = true;
  getByIdFilter({
    produceId: row.produceId,
    machineId: props.params.machineId,
  }).then((res) => {
    res.data.produceCoveringList.forEach((item) => {
      item.produceNo = row.produceNo;
      item.submitDate = row.submitDate;
      res.data.produceProductList.forEach((ele) => {
        if (item.orderProductId == ele.orderProductId) {
          item.productId = ele.productId;
          item.breedType = ele.breed;
        }
      });
      res.data.producePrintList.forEach((ele) => {
        if (item.orderProductId == ele.orderProductId) {
          // 覆膜张数 = 印数
          item.coveringPageNum = ele.printAmount;
        }
      });
    });

    paperList.value = res.data.produceCoveringList;
    bindList.value[0].pieceId = res.data.producePieceList[0]?.pieceId
    loadingInfo.value = false;
  });
};

// 选中
const handleSelectionChange = (row) => {
  selectRow.value = row;
  multiple.value = !row.length;
};
const handleSubmit = () => {
  selectRow.value = paperList.value.filter((item) => item.checked);
  emit("changeUpdate", selectRow.value);
};
const handleCancel = () => {
  paperList.value =[]
  proxy.resetForm("queryRef");
  emit("cancel");
};
const getList = async () => {
  loading.value = true;
  const res = await GetProducelistByMachineId({
    ...queryParams.value,
    machineId: userStore.workbranchId,
  });
  // orderListTable.value.setCurrentRow(res.rows[0])
  loading.value = false;
  orderList.value = res.rows;
  // if (orderList.value.length > 0) {
  //     handleClick(orderList.value[0])
  // }
  total.value = res.total;
};
// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};

// getList()
</script>

<style lang="scss" scoped>
.card {
  margin-top: 10px;
  --border-radius: 0.75rem;
  --primary-color: #7257fa;
  --secondary-color: #3c3852;
  width: 100%;
  font-family: "Arial";
  padding: 1rem;
  cursor: pointer;
  border-radius: var(--border-radius);
  background: #f4f4f8;
  box-shadow: 0px 8px 16px 0px rgb(0 0 0 / 3%);
  position: relative;
}

.card > * + * {
  margin-top: 1.1em;
}

.card .card__content {
  color: var(--secondary-color);
  font-size: 16px;
}

.card .card__title {
  padding: 0;
  font-size: 18px;
  font-weight: bold;
}

.card .card__date {
  color: #6e6b80;
  font-size: 0.8rem;
}

.card .card__arrow {
  color: #fff;
  position: absolute;
  background: var(--primary-color);
  padding: 0.4rem;
  border-top-left-radius: var(--border-radius);
  border-bottom-right-radius: var(--border-radius);
  bottom: 0;
  right: 0;
  transition: 0.2s;
  display: flex;
  justify-content: center;
  align-items: center;
}

.card svg {
  transition: 0.2s;
}

/* hover */
.card:hover .card__title {
  color: var(--primary-color);
  text-decoration: underline;
}

.card:hover .card__arrow {
  background: #70e786;
}

.card:hover .card__arrow svg {
  transform: translateX(3px);
}

.card-content {
  // max-width: 320px;
  position: relative;
  display: block;
  overflow: hidden;
  border: 1px solid rgba(243, 244, 246);
  border-radius: 0.5rem;
  background-color: #fff;
  padding: 5px;
}

.header {
  display: flex;
  justify-content: space-between;
  grid-gap: 1rem;
  gap: 1rem;
}

.title {
  font-size: 1.125rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: rgba(17, 24, 39, 1);
  text-decoration: none;
}

.title:hover {
  text-decoration: underline royalblue;
}

.name {
  margin-top: 0.25rem;
  font-size: 14px;
  line-height: 14px;
  font-weight: 500;
  color: rgba(75, 85, 99, 1);
}

.image {
  display: block;
  flex-shrink: 0;
  height: 30px;
  width: 30px;
  border-radius: 5px;
  object-fit: cover;
  background-color: royalblue;
  color: #fff;
  text-align: center;
  line-height: 30px;
}

.post-info {
  display: flex;
  grid-gap: 1rem;
  gap: 1rem;
}

.dt {
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 600;
  color: rgba(75, 85, 99, 1);
}

.dd {
  font-size: 0.75rem;
  line-height: 1rem;
  color: rgba(107, 114, 128, 1);
}
</style>
